package gang.com.frescodemo;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ViewUtils;
import android.util.Log;

import com.facebook.common.logging.FLog;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.image.QualityInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * Fresco国内最强的图片加载组件，两大优势，及时释放内存和空间，设计了三级缓存，两级内存一级文件
 * 特变内存管理：渐进式呈现图片，gif，webp格式
 * 1-----Fresco基本使用
 * 2-----渐进式加载图片
 * 3-----多图片请求
 * 4-----事件监听
 */
public class MainActivity extends AppCompatActivity {
    //Fresco的Github地址：https://github.com/facebook/fresco
    //国内API地址：http://fresco-cn.org/docs/index.html
    private String url = "http://7mno4h.com2.z0.glb.qiniucdn.com/560a26d2N78974496.jpg";
    private SimpleDraweeView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = (SimpleDraweeView) findViewById(R.id.my_image_view);
        moreImages();
        // requestImage();
       /* Uri uri = Uri.parse(url);

        view.setImageURI(uri);*/
    }

    /**
     * 渐进式图片加载方法，不要忘记需要在applicaton中配置一下
     */
    private void requestImage() {
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setProgressiveRenderingEnabled(true)
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(view.getController())
                .build();
        view.setController(controller);
    }

    /**
     * 不同分辨率图片请求加载方法
     */
    private void moreImages() {
        //事件监听部分
        ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(
                    String id,
                    @Nullable ImageInfo imageInfo,
                    @Nullable Animatable anim) {
                if (imageInfo == null) {
                    return;
                }
                QualityInfo qualityInfo = imageInfo.getQualityInfo();
                FLog.d("Final image received! " +
                                "Size %d x %d",
                        "Quality level %d, good enough: %s, full quality: %s",
                        imageInfo.getWidth(),
                        imageInfo.getHeight(),
                        qualityInfo.getQuality(),
                        qualityInfo.isOfGoodEnoughQuality(),
                        qualityInfo.isOfFullQuality());
            }

            @Override
            public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                //Log.d("Intermediate image received");
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                //Log.e(getClass(),  "Error loading %s")
            }
        };


        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setLowResImageRequest(ImageRequest.fromUri(url))
                .setImageRequest(ImageRequest.fromUri(url + "/test"))
                //.setOldController(view.getController())
                .setControllerListener(controllerListener)
                .build();
        view.setController(controller);
    }
}
